package com.ushio.gougouwangguo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushio.gougouwangguo.entity.mysql.Materials;
import com.ushio.gougouwangguo.entity.qo.MaterialsUpDateQo;
import com.ushio.gougouwangguo.entity.vo.MaterialsVo;
import com.ushio.gougouwangguo.mapper.MaterialsMapper;
import com.ushio.gougouwangguo.service.MaterialsService;
import com.ushio.gougouwangguo.utils.MyBeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MaterialsServiceImpl extends ServiceImpl<MaterialsMapper, Materials> implements MaterialsService {

    @Autowired
    MaterialsMapper materialsMapper;


    @Override
    public List<MaterialsVo> getMaterialsAll() {

        List<Materials> materialsList = materialsMapper.selectList(new LambdaQueryWrapper<Materials>().eq(Materials::isRemove,false));

        List<MaterialsVo> materialsVoList = materialsList.stream().map(materials -> {

            MaterialsVo materialsVo = MaterialsVo.builder().build();

            BeanUtils.copyProperties(materials, materialsVo);

            return materialsVo;

        }).collect(Collectors.toList());

        return materialsVoList;
    }

    @Override
    public Page<MaterialsVo> getMaterialsPage(Page<Materials> pageParam,String materialsName, Byte materialsQualityId, String materialsTypeId, String materialsDescribe, String materialsRole) {

        LambdaQueryWrapper<Materials> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(materialsName!=null,Materials::getMaterialsName,materialsName);
        lambdaQueryWrapper.eq(materialsQualityId!=null,Materials::getMaterialsQualityId,materialsQualityId);
        lambdaQueryWrapper.eq(materialsTypeId!=null,Materials::getMaterialsTypeId,materialsTypeId);
        lambdaQueryWrapper.like(materialsDescribe!=null,Materials::getMaterialsDescribe,materialsDescribe);
        lambdaQueryWrapper.like(materialsRole!=null,Materials::getMaterialsRole,materialsRole);
        lambdaQueryWrapper.eq(Materials::isRemove,false);

        Page<Materials> materialsPage = materialsMapper.selectPage(pageParam, lambdaQueryWrapper);

        List<MaterialsVo> materialsVoList = materialsPage.getRecords().stream().map(new Function<Materials, MaterialsVo>() {
            @Override
            public MaterialsVo apply(Materials materials) {
                MaterialsVo materialsVo = MaterialsVo.builder().build();
                BeanUtils.copyProperties(materials, materialsVo);
                return materialsVo;
            }
        }).collect(Collectors.toList());

        Page<MaterialsVo> materialsVoPage = MyBeanUtils.convertPage(pageParam, materialsVoList);

        return materialsVoPage;
    }

    @Override
    public void updateMaterials(MaterialsUpDateQo materialsUpDateQo) {

        Materials materials = Materials.builder().build();

        BeanUtils.copyProperties(materialsUpDateQo,materials);

        if (materials.getMaterialsId()!=null){
            materialsMapper.updateById(materials);
        }else{
            materialsMapper.insert(materials);
        }

    }

    @Override
    public void deleteMaterials(MaterialsUpDateQo materialsUpDateQo) {

        Materials materials = Materials.builder().materialsId(materialsUpDateQo.getMaterialsId()).remove(true).build();

        materialsMapper.updateById(materials);
    }
}
